package com.ma.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.io.UnsupportedEncodingException;

/**
 * author: MaHX
 * date: 2019/5/30/15:16
 * description:
 **/
public class IntersmsUtil {

    /**
     * 根据手机号码发送短信
     *
     *
     */
    public String SendSms(String mobile,String content,String msg){
        //请求地址
        String url="****";

        //API账号，50位以内。必填
        String account="****";

        //API账号对应密钥，联系客服获取。必填
        String password="****";

        //组装请求参数
        JSONObject map=new JSONObject();
        map.put("account", account);
        map.put("password", password);
        map.put("msg", msg);
        map.put("mobile", mobile);

        String params=map.toString();
        System.out.println("请求参数为:" + params);
        String result=HttpUtil.post(url, params);
        System.out.println("返回参数为:" + result);
        JSONObject jsonObject =  JSON.parseObject(result);
        String code = jsonObject.get("code").toString();
        String msgid = jsonObject.get("msgid").toString();
        String error = jsonObject.get("error").toString();
        System.out.println("错误提示"+error);
        System.out.println("状态码:" + code + ",状态码说明:" + error + ",消息id:" + msgid);
        if(error == ""||error.length()==0){
//					String  Acode= CommonUtil.MD5(scode);   //MD5加密验证码
            System.out.println("验证码："+content);
            return content;
        }
        else{
            System.out.println("短信发送失败，"+error+"code:"+code);
            error="fail";
            return error;
        }
    }



    public static void main(String[] args) throws UnsupportedEncodingException {
        String phone = "8618621085039";
        String content="123";
        String msg = "taianquan";
        IntersmsUtil util=new IntersmsUtil();
        util.SendSms(phone,content,msg);
    }
}